package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceGetByPurchaseIdDTO;
import com.xbongbong.pro.invoice.pojo.vo.InvoiceGetByPurchaseIdVO;
import com.xbongbong.pro.purchaseinvoice.dto.PurchaseInvoiceAmountDetailGetDTO;
import com.xbongbong.pro.purchaseinvoice.vo.PurchaseInvoiceAmountDetailVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.service.PurchaseInvoiceService;
import com.xbongbong.saas.service.SaasListService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 发票
 *
 * @author yuqian
 * @version v1.0
 * @date 2020/2/28 17:27
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/purchaseInvoice")
public class PurchaseInvoiceController {

    @Resource
    private PurchaseInvoiceService purchaseInvoiceService;
    @Resource
    private SaasListService saasListService;

    /**
     * 根据客户ID查询发票列表
     * @param purchaseInvoiceAmountDetailGetDTO 入参
     * @param br 校验信息
     * @author zhouwq
     * @date 2019/11/20 18:48
     */
    @RequestMapping(value = "/amountDetail/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getByContract(@RequestBody @Valid PurchaseInvoiceAmountDetailGetDTO purchaseInvoiceAmountDetailGetDTO, BindingResult br) {
        XbbResponse<PurchaseInvoiceAmountDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //为了区分 编辑获取金额明细 或者 新建获取金额明晰
                purchaseInvoiceAmountDetailGetDTO.setIsGetDetail(BasicConstant.ONE);
                PurchaseInvoiceAmountDetailVO purchaseInvoiceInvoiceAmountDetailVO = purchaseInvoiceService.getPurchaseInvoiceAmountDetail(purchaseInvoiceAmountDetailGetDTO);
                response = new XbbResponse<>(purchaseInvoiceInvoiceAmountDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 根据采购合同ID查询发票列表
     * @param invoiceGetByPurchaseIdDTO 入参
     * @param br 校验信息
     * @author zhouwq
     * @date 2019/11/20 18:48
     */
    @RequestMapping(value = "/list/getByPurchase", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getByContract(@RequestBody @Valid InvoiceGetByPurchaseIdDTO invoiceGetByPurchaseIdDTO, BindingResult br) {
        XbbResponse<InvoiceGetByPurchaseIdVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try{
                InvoiceGetByPurchaseIdVO invoiceGetByPurchaseIdVO = purchaseInvoiceService.getByPurchase(invoiceGetByPurchaseIdDTO);
                response = new XbbResponse<>(invoiceGetByPurchaseIdVO);
            }catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
